package com.synway.service.impl;


import com.synway.handler.SpiderHandler;
import com.synway.repository.HouseRepository;
import com.synway.service.HouseService;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.File;

@Service
public class HouseServiceImpl implements HouseService{
    @Value("${python.rootPath}")
    private String rootPath;
    @Value("${python.env}")
    private String pythonEnv;
    @Autowired
    private AmqpTemplate rabbitTemplate;

    @Autowired
    private HouseRepository houseRepository;

    @Override
    public void spiderToDB(String spider) {
        SpiderHandler.spiderHandler(rootPath,pythonEnv,spider+"_to_db.py");
        SpiderHandler.rabbitTemplate = rabbitTemplate;
    }

    @Override
    public void deleteData(String table,String date) {
        houseRepository.deleteData(table,date);
    }

    @Override
    public void deleteFiles(String date, String spider) {
        File f = new File(rootPath+"/data/ke/"+spider+"/cd/"+date);
        this.deleteFlolder(f);
        f.delete();
    }

    private void deleteFlolder(File floder){
        File[] files = floder.listFiles();
        if(files != null){
            for(File f:files){
                if(f.isDirectory()){
                    deleteFlolder(f);
                }else{
                    f.delete();
                }
            }
        }
    }
}
